/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Gsb.Andro;

import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import modele.DAO;
import modele.Med;

/**
 *
 * @author plasalmonie
 */
public class ListeMedActivity extends ListActivity {

    private List<Med> lesMeds;

    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        //on va chercher la vue 
        setContentView(R.layout.listemed);
        Button filtre = (Button) findViewById(R.id.buttonFiltre);
        final EditText LeNomChercher = (EditText) findViewById(R.id.EditerNom);
        TextView label = (TextView) findViewById(R.id.label);
        Intent inter = getIntent();
        String numDep = inter.getStringExtra("leDep");
        lesMeds = DAO.getLesMeds(numDep);
        label.setText("Liste des Médecins du departement : " + numDep);
        MedAdapter adapter = new MedAdapter(lesMeds, this);
        setListAdapter(adapter);
        
        filtre.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                List<Med> lesMedFiltre = new ArrayList<Med>();
                String nomCherche = LeNomChercher.getText().toString().toUpperCase();
                for (Med unMed : lesMeds) {
                    if (unMed.getNom().startsWith(nomCherche)) {
                        lesMedFiltre.add(unMed);
                    }
                }
                MedAdapter adapter = new MedAdapter(lesMedFiltre, getApplicationContext());
                setListAdapter(adapter);
            }
        });
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        //création d'une interface vers une autre activités 
        Intent inter = new Intent(this, FicheMed.class);
        //preparation d'une donnée qui sera passée à l'autre
        Med leMed = (Med) l.getItemAtPosition(position);
        String prenom = leMed.getPrenom();
        String nom = leMed.getNom();
        String adresse = leMed.getAdresse();
        String spe = leMed.getSpe();
        String num = leMed.getNumero();
        inter.putExtra("leNom", nom);
        inter.putExtra("lePrenom", prenom);
        inter.putExtra("adr", adresse);
        inter.putExtra("laSpe", spe);
        inter.putExtra("leNum", num);
        //lancement de l'activity
        startActivity(inter);
    }
}
